<?php
/*
Plugin Name: BPDEV-TINYMCE
Plugin URI: http://bp-dev.org/projects/#tinymce
Description: Adds visual editor feature to wire post forms
Author: Nicola Greco
Version: 0.3
Author URI: http://nicolagreco.com

Copyright (c) 2009-2011, Nicola Greco (mail: notsecurity@gmail.com | website: http://nicolagreco.com).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
/**
 * This plugin adds a visual editor to wire forms
 *
 * @package BPDEV-PLUGINS
 * @param    type    $varname    Description
 * @return   type                Description
 * @author Nicola Greco
 * @link http://nicolagreco.com
 * @link http://buddypressdev.org/plugins/bpdev-tinymce
 */

define( 'BPDEV_TINYMCE_NAME', 'BPDEV-TINYMCE' );
define( 'BPDEV_TINYMCE_VERSION', '0.3' );
define( 'TINYMCE_VERSION', '2.1.1' );

require_once( 'bp-core.php' );
require_once( 'bpdev-core.php' );

if ( !get_option( 'bpdev-tinymce-status' ) ) add_option( 'bpdev-tinymce-status', 'off' );
if ( get_option('bpdev-tinymce-status') == "on" ) {

	add_action('bp_wire_custom_wire_boxes_before', 'bpdev_tinymce_init', 1);
	add_action('bp_wire_custom_wire_boxes_before', 'bpdev_tinymce_script', 2);

	add_action('groups_forum_new_topic_before', 'bpdev_tinymce_init', 1);
	add_action('groups_forum_new_topic_before', 'bpdev_tinymce_script', 2);

	add_action('groups_forum_new_reply_before', 'bpdev_tinymce_init', 1);
	add_action('groups_forum_new_reply_before', 'bpdev_tinymce_script', 2);

	add_action('bpdev_tinymce_advanced', 'bpdev_tinymce_advanced_custom', 1);

}

add_action( 'bpdev_admin_plugins_screen', 'bpdev_tinymce_admin_menu_screen', 3 );
add_action( 'bpdev_admin_plugins_save', 'bpdev_tinymce_admin_menu_save', 3 );

add_action( 'wp', 'bpdev_tinymce_setup_globals', 1 );
add_action( '_admin_menu', 'bpdev_tinymce_setup_globals', 1 );

function bpdev_tinymce_setup_globals() {

	global $bp;

	$bp->bpdev->tinymce->slug = 'tinymce';
	$bp->bpdev->tinymce->js = '/wp-content/mu-plugins/bpdev-tinymce/tiny_mce.js';

}

function bpdev_tinymce_admin_menu_save() {

	do_action( 'bpdev_admin_save_component', 'bpdev-tinymce-status' );

}

function bpdev_tinymce_admin_menu_screen() {

	?>
	<h3><?php echo BPDEV_TINYMCE_NAME; ?></h3>
	<table class="form-table">
		<tr valign="top">
			<th scope="row">Plugin Version</th>
			<td>
				<?php echo BPDEV_TINYMCE_VERSION; ?>
			</td>
		</tr>
		<tr>
			<th scope="row">TinyMCE Version</th>
			<td>
				<?php echo TINYMCE_VERSION; ?>
			</td>
		</tr>
		<tr>
			<th scope="row">Plugin Status</th>
			<td>
				<select id="bpdev-tinymce-status" name="bpdev-tinymce-status">
					<option value="off"  <?php if ( get_option( 'bpdev-tinymce-status' ) == "off" || !get_option( 'bpdev-tinymce-status' ) ) echo 'selected=""'; ?>>Off</option>
					<option value="on" <?php if ( get_option('bpdev-tinymce-status') == "on") echo 'selected=""'; ?>>On</option>
				</select>
			</td>
		</tr>
	</table>
	<?php
}

function bpdev_tinymce_init() {

	global $bp;
	?>
	<script type="text/javascript" src="<?php echo $bp->bpdev->tinymce->js; ?>"></script>
	<?php

}

function bpdev_tinymce_script() {

	?>
	<script type="text/javascript">
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced"<?php if (has_action('bpdev_tinymce_advanced')) echo ",\n"; ?>

			<?php do_action('bpdev_tinymce_advanced'); ?>
		});
	</script>
	<?php

}

function bpdev_tinymce_advanced_custom() {
	?>
	theme_advanced_buttons1 : "bold,italic,underline,link,unlink,bullist,blockquote,undo", 
	theme_advanced_buttons2 : "", 
	theme_advanced_buttons3 : "" 
	<?php
}
?>